package com.example.demo01

import android.os.Bundle
import android.widget.SimpleAdapter
import android.widget.Spinner
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ArrayAdapterActivity : AppCompatActivity() {
    //0.准备城市数据
    data class CityData(val icon: Int, val name: String, val intro: String)

    //1.创建数据源
    private val cityList = listOf(
        CityData(R.drawable.bj, "北京", "中国首都，历史悠久，政治文化中心，古韵今风交融。"),
        CityData(R.drawable.bj, "上海", "中国东部国际大都市，金融中心，多元文化汇聚。"),
        CityData(R.drawable.bj, "广州", "中国南部沿海城市，经济发达，文化多元。"),
        CityData(R.drawable.bj, "深圳", "中国南部科技创新中心，现代化城市。"),
        CityData(R.drawable.bj, "杭州", "中国东南部历史文化名城，风景秀丽。"),
        CityData(R.drawable.bj, "南京", "中国东部历史文化名城，教育科研中心。"),
    )

    private lateinit var spCity: Spinner

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_array_adapter)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        spCity = findViewById(R.id.spinner_city_plus)
        spCity.adapter = SimpleAdapter(
            this,
            cityList.map {
                mapOf("icon" to it.icon, "name" to it.name, "intro" to it.intro)
            },
            R.layout.item_icon,
            arrayOf("icon", "name", "intro"),
            intArrayOf(R.id.cityIcon, R.id.cityText, R.id.cityIntro)
        )
    }
}